package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParksModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Sawior
 */
@Stateful
public class ParksDAOImpl implements ParksDAO {

    private EntityManager entityManager;
    
    public ParksDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }
    
    @Override
    public List<ParksModel> getAllParks() {
        return (List<ParksModel>) entityManager.createNamedQuery("ParksModel.findAll").getResultList();
    }

    @Override
    public int getParksSize() {
        return entityManager.createNamedQuery("ParksModel.findAll").getResultList().size();
    }

    @Override
    public ParksModel getParksById(Integer id) {
        return (ParksModel) entityManager.createNamedQuery("ParksModel.findByParkId").setParameter("parkId", id).getSingleResult();
    }

    @Override
    public List<ParksModel> getParksByParkDate(Date parkDate) {
        return (List<ParksModel>) entityManager.createNamedQuery("ParksModel.findByParkDate").setParameter("parkDate", parkDate).getResultList();
    }

    @Override
    public List<ParksModel> getParksByType(String type) {
        return (List<ParksModel>) entityManager.createNamedQuery("ParksModel.findByType").setParameter("type", type).getResultList();
    }

    @Override
    public void addPark(ParksModel park) {
        entityManager.getTransaction().begin();
        entityManager.persist(park);
        entityManager.flush();
        entityManager.refresh(park);
        entityManager.persist(park);
        entityManager.getTransaction().commit();
    }

    @Override
    public void updatePark(ParksModel park) {
        entityManager.getTransaction().begin();
        entityManager.persist(park);
        entityManager.flush();
        entityManager.refresh(park);
        entityManager.persist(park);
        entityManager.getTransaction().commit();
    }

    @Override
    public void removePark(Integer id) {
        ParksModel rowToDelete = getParksById(id);
        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }
    
    public EntityManager getEntityManager() {
        return entityManager;
    }

    @Override
    public int getByParkDateSize(Date parkDate) {
    List <ParksModel> list = (List<ParksModel>) entityManager.createNamedQuery("ParksModel.findByParkDate").setParameter("parkDate", parkDate).getResultList();
    return list.size();
    }

    @Override
    public List<ParksModel> getParksByClient(ClientsModel cl) {
         List <ParksModel> list = (List<ParksModel>) entityManager.createNamedQuery("ParksModel.findByClient").setParameter("clientId", cl).getResultList();
    return list;
    }

    @Override
    public List<ParksModel> getParksByClientAndType(ClientsModel cl, String type) {
       List <ParksModel> list = (List<ParksModel>) entityManager.createNamedQuery("ParksModel.findByClientAndType").setParameter("clientId", cl).setParameter("type", type).getResultList();
    return list;
    }

    
}
